//
//  EssenceCell.swift
//  FM
//
//  Created by x on 2017/10/13.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class EssenceCell: UITableViewCell {
    
    /// 保存配图的宽度约束
    var pictureWidthCons: NSLayoutConstraint?
    /// 保存配图的高度约束
    var pictureHeightCons: NSLayoutConstraint?
    /// 保存配图的顶部约束
    var pictureTopCons: NSLayoutConstraint?


    var model:Essence? {
        didSet {
            //赋值
            iconImage.sd_setImage(with: model?.user?.avaterURL)
            nickNameLable.text = model?.user?.nickname
            commentLable.text = "评论: " + (model?.commentnum)!
            updateTimeLable.text = model?.updated
            titleLable.text = model?.title
            contentLable.text = model?.content
            
            
            //设置配图的相应属性
            //设置完约束之后要赋值 (如果有转发则赋值转发的model 没有转发则赋值原创的model)
            pictureView.model = model
            //1.根据模型计算配图尺寸 (注意计算尺寸需要用到我们的一个数据模型)
            let size = pictureView.calculateImageSize() //得到配图的尺寸
            //2设置配图的尺寸
            pictureWidthCons?.constant = size.width
            pictureHeightCons?.constant = size.height
            pictureTopCons?.constant = size.height == 0 ? 0 :10
        }
    }
    
    /// 标记头像是否显示圆角 默认是显示圆角(true)
    var isCornerRadius:Bool = true {
        didSet {
            guard isCornerRadius else {
                iconImage.layer.cornerRadius = 0
                return
            }
        }
    }
    
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //1.初始化
        setupUI()
    }
    
    func setupUI() -> Void {
        
        //1.添加子控件
        contentView.addSubview(iconImage)
        contentView.addSubview(nickNameLable)
        contentView.addSubview(commentLable)
        contentView.addSubview(updateTimeLable)
        contentView.addSubview(titleLable)
        contentView.addSubview(contentLable)
        contentView.addSubview(bgLbale)
        contentView.addSubview(pictureView)
        
        //2.布局子控件
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        
        let dics = ["iconImage":iconImage,
                    "nickNameLable":nickNameLable,
                    "commentLable":commentLable,
                    "updateTimeLable":updateTimeLable,
                    "titleLable":titleLable,
                    "contentLable":contentLable,
                    "pictureView":pictureView,
                    "bgLbale":bgLbale] as [String:Any]
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[iconImage(50)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[iconImage(50)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[iconImage]-5-[nickNameLable]", options: NSLayoutFormatOptions.alignAllTop, metrics: nil, views: dics)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[commentLable]-40-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-15-[commentLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[iconImage]-5-[updateTimeLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[nickNameLable]-5-[updateTimeLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[titleLable]-10-|", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: dics)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[updateTimeLable]-5-[titleLable]", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: dics)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[contentLable]-20-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[titleLable]-15-[contentLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        
        
        pictureWidthCons = NSLayoutConstraint.init(item: pictureView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 0)
        pictureHeightCons = NSLayoutConstraint.init(item: pictureView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 0)
        pictureView.addConstraint(pictureWidthCons!)
        pictureView.addConstraint(pictureHeightCons!)

        pictureTopCons = NSLayoutConstraint.init(item: pictureView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: contentLable, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 10)
        contentView.addConstraints([NSLayoutConstraint.init(item: pictureView, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.left, multiplier: 1.0, constant: 20)])
        contentView.addConstraint(pictureTopCons!)
        
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[bgLbale]-10-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[pictureView]-10-[bgLbale(1)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dics)

        
        contentView.addConstraints(cons)
        
    }
    
    func rowHeight(essence:Essence) -> CGFloat {
        
        //0.根据model重新计算cell的高度
        model = essence
        
        //1.重新布局
        layoutIfNeeded()
        
        //2.返回视图的高度
        return bgLbale.frame.maxY
    }
    
    // MARK:  -- 懒加载
    
    /// 头像图片
    private lazy var iconImage:UIImageView = UIImageView.imageWith(radios: 25, imageName: "")
    
    /// 昵称
    private lazy var nickNameLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 12)
    
    /// 评论
    private lazy var commentLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 12)
    
    /// 发布时间
    private lazy var updateTimeLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.RGBColor(r: 173, g: 173, b: 173), fontSize: 12)
    
    /// 标题
    private lazy var titleLable:UILabel = {
        let tt = UILabel.lableWith(title: "", titleColor: UIColor.RGBColor(r: 177, g: 74, b: 72), fontSize: 14)
        tt.numberOfLines = 0
        tt.lineBreakMode = NSLineBreakMode.byWordWrapping
        
        return tt
    }()
    
    /// 内容
    private lazy var contentLable:UILabel = {
        let tt = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 14)
        tt.numberOfLines = 0
        tt.lineBreakMode = NSLineBreakMode.byWordWrapping
        
        //调整lable的行间距(有问题)
//        let attributeString = NSMutableAttributedString()
//        let paragraphStyle = NSMutableParagraphStyle()
//        paragraphStyle.lineSpacing = 2
//        attributeString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSRange.init(location: 0, length: (tt.text! as NSString).length))
//        tt.attributedText = attributeString
        
        return tt
    }()
    
    private lazy var bgLbale:UILabel = UILabel.lableWith(backgroundColor: UIColor.RGBColor(r: 162, g: 162, b: 162))
    
    private lazy var pictureView:EssencePictureView = EssencePictureView()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
